﻿<template>
  <div>
    <el-row :gutter="20">
      <el-col offset="3" :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>修改密码</span>
          </div>

              <el-form ref="form" :model="user" :rules="rules" label-width="80px">
                <el-form-item label="用户名" prop="nickName">
                  <el-input v-model="user.name" />
                </el-form-item>
                <el-form-item label="旧密码" prop="oldPassword">
                  <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" />
                </el-form-item>
                <el-form-item label="新密码" prop="password">
                  <el-input v-model="user.password" placeholder="请输入新密码" type="password" />
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                  <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="mini" @click="updatePassword">保存</el-button>
<!--                  <el-button type="danger" size="mini" @click="close">关闭</el-button>-->
                </el-form-item>
              </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
    export default {
      $$route:{
        meta:{
          title:'个人信息'
        }
      },
        name: "UserInfo",
        data() {
            return {
              activeTab:'userinfo',
                isLoading:false,
                roleList: [],
                levelList: [],
                userDlgVisible:false,
                user:{},
                loadingUserInfo:false,
            }
        },
        computed: {},
        created: function () {
            if (!this.$route.query.id) {
                this.user = this.$store.state.user;
            }
        },
        methods: {
          updatePassword(){
            if(this.user.password!=this.user.confirmPassword) {
              this.$message.warning('两次密码输入不一致');
              return
            }

            let userData = JSON.parse(JSON.stringify(this.user));
            let rsaPassword = this.$encryption(userData.password);
            userData.password = rsaPassword;
            let oldRsaPassword = this.$encryption(userData.oldPassword);
            userData.oldPassword = oldRsaPassword;
            this.$post('/user/update_password',userData,(response)=>{
              this.$message.success('修改成功');
              this.user.password='';
              this.user.oldPassword='';
              this.user.confirmPassword='';
            });
          },

            openEditDlg(item){
                this.userDlgVisible = true;
                this.loadingUserInfo = true;
                const message = this.$message;
                this.$get("/user/info/"+item.id,{},(data) => {
                    var userData = data.data;
                    var roleIdList = [];
                    if(userData.roleList) {
                        for(var item of userData.roleList) {
                            roleIdList.push(item.id);
                        }
                    }
                    userData.roleIdList = roleIdList;
                    this.user = userData;
                    this.user.editMode=true;
                },()=>{this.loadingUserInfo = false;});

                // this.user = JSON.parse(JSON.stringify(item));

            },
            saveUser: function(id){
                const message = this.$message;
                if(this.user.id) {
                    this.$post("/user/update", this.user, (data) => {
                        this.userDlgVisible = false;
                        message.success('更新成功！');

                        this.loadUserList();
                    });
                } else {
                    this.$post("/user/create", this.user, (data) => {
                        this.userDlgVisible = false;
                        message.success('创建成功！');
                        this.loadUserList();

                    });
                }
            },
            loadRoleData:function () {
              const message = this.$message;
              this.$get("/role/data",{},(data) => {
                this.roleList = data.data;
              });
            },

            handleClickRow(row,column,event) {

            },

            loadCompanyList() {
                this.$get("/company/tree",{},(data) => {

                    var map = {};
                    for(var company of data.data) {
                        map[company.id] = company;
                        if(company.children&&company.children.length>0) {
                            for(var subcompany of company.children) {
                                map[subcompany.id] = subcompany;
                            }
                        }
                    }

                    this.companyMap = map;
                    this.companyList = data.data;
                });
            },
        },

    }
</script>
